package com.dlz.systemshop.config;

import com.dlz.systemshop.enums.ResponseCodeEnum;
import com.dlz.systemshop.pojo.APIResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@Slf4j
@RestControllerAdvice
public class ResponseErrorAdvice {

    @ExceptionHandler
    public APIResponse<?> handler(Exception e) {
        log.error("出现异常 , e: ", e);
        return APIResponse.error(ResponseCodeEnum.ERROR.getCode(), "出现异常 e: " + e.getMessage());
    }

    @ExceptionHandler
    public APIResponse<?> handler(JsonProcessingException e) {
        log.error("body 转换失败 , e: {}", e.getMessage());
        return APIResponse.error(ResponseCodeEnum.ERROR.getCode(), "出现异常 e: " + e.getMessage());
    }

    @ExceptionHandler
    public APIResponse<?> handler(MaxUploadSizeExceededException e) {
        log.error("文件太大 , e: {}", e.getMessage());
        return APIResponse.error(ResponseCodeEnum.ERROR.getCode(), "文件太大");
    }

}
